package com.mdh.android.vm.util;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mdh.android.vm.R;

public class SingleTextAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<String> mData;
    private int mColor = 0x12345678;
    private int mHeight = -1;
    private int mSize = -1;
    private int mPos = 0;
    private boolean mSel = false;

    public SingleTextAdapter(Context context, List<String> data) {
	this.mInflater = LayoutInflater.from(context);
	mData = data;
    }

    public void setTextColor(int color) {
	mColor = color;
    }

    public void setTextSize(int size) {
	mSize = size;
    }

    public void setItemMinHeight(int height) {
	mHeight = height;
    }

    public void setSelection(int position) {
	mPos = position;
	mSel = true;
	this.notifyDataSetChanged();
    }

    public int getSelection() {
	return mPos;
    }

    @Override
    public int getCount() {
	return mData.size();
    }

    @Override
    public Object getItem(int arg0) {
	return null;
    }

    @Override
    public long getItemId(int arg0) {
	return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
	ViewHolder holder = null;

	if (convertView == null) {
	    holder = new ViewHolder();
	    convertView = mInflater.inflate(R.layout.singletext_layout, null);
	    holder.title = (TextView) convertView.findViewById(R.id.title);
	    if (mColor != 0x12345678)
		holder.title.setTextColor(mColor);
	    if (mSize != -1)
		holder.title.setTextSize(mSize);
	    if (mHeight != -1)
		holder.title.setMinHeight(mHeight);
	    convertView.setTag(holder);
	} else {
	    holder = (ViewHolder) convertView.getTag();
	}

	if (mSel) {
	    if (position == mPos)
		convertView.setBackgroundColor(Color.GREEN);
	    else
		convertView.setBackgroundColor(Color.TRANSPARENT);
	}

	holder.title.setText(mData.get(position));

	return convertView;
    }

    public final class ViewHolder {
	public TextView title;
    }

}
